import requests
"""
pytest 在执行的时候会自动将test开头的函数作为测试用例来执行
"""
def testabcde():
    assert True
#测试用例
def test_topic():
    #发送get请求
    query_data = {
        "page":1,
        "tab":"ask",
        "limit":2
    }
    res = requests.get("http://49.233.108.117:3000/api/v1/topics",params=query_data)
    print(res,type(res))
    #服务器响应结果转换为 字典格式
    print(res.json())
    #assert 断言
    #1 数据长度应该为2
    jsonData = res.json()
    assert len(jsonData['data']) ==2

    #2.data 数据中的tab值应该都为ask
    data = jsonData['data']
    for d in data:
        assert d["tab"] == "ask"
body_data = {
        "accesstoken":"48a14c9d-20b7-4714-90ac-3edae14e138e",
        "title":"helloworld",
        "tab":"ask",
        "content":"xxxxxxxxxxxxx"
    }
#测试发布话题
def create_a_topic():

    res = requests.post(url="http://49.233.108.117:3000/api/v1/topics",data=body_data)
    return res



#测试发布话题
def test_create_topic():
    res = create_a_topic()
    print(res.json())
    jsonData = res.json()
    print('jsondata',jsonData)
    assert jsonData['success'] == True
    assert not jsonData['topic_id'] is None#!=None 也可以
    #二次发帖内容应该不相等

    res2 = create_a_topic()
    jsonData2 = res2.json()
    assert jsonData != jsonData2

def test_topic_detail():
    # 1.创建话题
    res = create_a_topic()
    topic_id = res.json()['topic_id']
    # 2.访问主题详情
    res2 = requests.get(f'http://49.233.108.117:3000/api/v1/topic/{topic_id}')
    print(res2.json())
    jsonData = res2.json()
#断言topic_id 应该跟发帖时保持一致
    assert topic_id == jsonData['data']["id"]
    #tab值应该与发帖时tab保持一致
    assert body_data["tab"] == jsonData["data"]["tab"]
    # title值
    assert body_data["title"] == jsonData["data"]["title"]
    # content值
    assert body_data['content'] in jsonData["data"]['content']

